package bd.proiect;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;


public class AddGenus extends Activity{
	
	private String name;
	private int family_id;
	private int origin_id;
	private String soil;
	private String temp;
	private String water;
	
	
	EditText ed_name;
	AutoCompleteTextView auto_fam;
	AutoCompleteTextView auto_or;
	EditText ed_soil;
	EditText ed_temp;
	EditText ed_water;
	
	ArrayList<String> FAM;
    ArrayList<String> ORI;
	
	DataBase bs;
	
	@Override
	 public void onCreate(Bundle savedInstanceState) {
	     super.onCreate(savedInstanceState);
	     setContentView(R.layout.addgenus);
	     
	     ed_name = new EditText(this);
         ed_name = (EditText)findViewById(R.id.addgenus_name);
         
         ed_soil = new EditText(this);
         ed_soil = (EditText)findViewById(R.id.addgenus_soil);
         
         ed_temp = new EditText(this);
         ed_temp = (EditText)findViewById(R.id.addgenus_temp);
         
         ed_water = new EditText(this);
         ed_water = (EditText)findViewById(R.id.addgenus_water);
         
         
        bs = new DataBase(this);
 	    try{
			bs.createDataBase();
			bs.openDataBase();
			FAM = bs.getFamArray();
			ORI = bs.getOriArray();
			bs.close();
 	    }catch(Exception e){}
 	    
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, FAM);
        auto_fam = (AutoCompleteTextView)
                findViewById(R.id.select_family);
        auto_fam.setAdapter(adapter);
         
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, ORI);
        auto_or = (AutoCompleteTextView)
                findViewById(R.id.select_origin);
        auto_or.setAdapter(adapter2);
        
        
	     Button SaveButton = (Button) findViewById(R.id.addgenus_button);
	     SaveButton.setOnClickListener(new OnClickListener() {
	         public void onClick(View v) {
	        	 
	        		name = ed_name.getText().toString();
	        		soil = ed_soil.getText().toString();
	        		temp = ed_temp.getText().toString();
	        		water = ed_water.getText().toString();
	    	   
	    	    try{
	    				bs.createDataBase();
	    				bs.openDataBase();
		        		family_id = bs.getFamilyID(auto_fam.getText().toString());
		        		origin_id = bs.getOriginID(auto_or.getText().toString());
	    				bs.insertGenus(name,family_id,origin_id,soil,"medium",temp,water);
	    				bs.close();
	    				
	    		}catch(Exception e){
	    			ed_name.setText("buba" + e);
	    		}
	    		finish();
	         }
	     });
	     
	     }

}
